package kr.co.yhcho.adoration.db;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBHandler {
	private DBHelper helper = null;
	private SQLiteDatabase db = null;

	public DBHandler(Context mContext) {
		this.helper = new DBHelper(mContext);
		this.db = helper.getWritableDatabase();
	}

	public static DBHandler open(Context mContext) throws SQLException {
		DBHandler handler = new DBHandler(mContext);

		return handler;
	}

	public void close() {
		helper.close();
	}

	public Cursor selectFromTitle() throws SQLException {
		Cursor cursor = db.rawQuery(
				"select _id, title, subTitle from adorationTitle;", null);

		if (cursor != null) {
			cursor.moveToFirst();
		}

		return cursor;
	}

	public Cursor selectFromBody(long titleID) throws SQLException {
		Cursor cursor = db
				.rawQuery(
						"select _id, body, book, page, startLine, endLine, titleID from adorationBody where titleID = "
								+ titleID + ";", null);

		if (cursor != null) {
			cursor.moveToFirst();
		}

		return cursor;
	}

	public Cursor selectFromSubTitle(long id) throws SQLException {
		Cursor cursor = db.rawQuery(
				"select title, subTitle from adorationTitle where _id = " + id + ";",
				null);

		if (cursor != null) {
			cursor.moveToFirst();
		}

		return cursor;
	}
}
